package com.cskaoyan;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;

/**
 * 创建日期: 2022/10/14 15:04
 *
 * @author ciggar
 */
public class MybatisMain {

    public static void main(String[] args) throws IOException {

        // 1. 获取SqlSessionFactoryBuilder
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();

        // 2. 通过SqlSessionFactoryBuilder来获取SqlSessionFactory
        InputStream in = MybatisMain.class.getClassLoader().getResourceAsStream("mybatis-config.xml");

        InputStream in2 = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = builder.build(in);


        // 3. 获取SqlSession
        // 这个SqlSession就相当于Connection对象

        // 不会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 可以自动提交
        SqlSession sqlSessionWithAutoCommit = sqlSessionFactory.openSession(true);


        // 4. 执行对应的SQL语句
//        int affectedRows = sqlSession.insert("cskaoyan.aaa");
//
//        System.out.println("affectedRows:" + affectedRows);
//
//        sqlSession.commit();

        int affectedRows = sqlSession.update("cskaoyan.updateUser");
        sqlSession.commit();


    }
}
